<?php
namespace app\manage\event;
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/10/11
 * Time: 11:58
 * 上传图片到微信服务器 可上传多张图片
 */
vendor('zoujingli.wechat-php-sdk.include');
class Weixin
{
    private $pic_arr = [];//返回本地与微信图片对应数组
    private $weixin_pic = [];//保存微信上传的图片路径
    private $config;
    private $wechat;
    public function init($api='Media'){
        $this->config = getWeixinConfig();
        if($this->config){
            $this->wechat = & \Wechat\Loader::get_instance($api, $this->config);
            return $this->wechat;
        }else{
            return '';
        }

    }

    /**
     * @param $img
     * @return array
     * 上传内容中的图片
     */
    public function uploadImgToWeixin($img){
        if(is_array($img)){
            $this->pic_arr = $img;
            foreach($img as $k=>$v){
                $up['media'] = '@'.getcwd().str_replace(config('domain.pic'),'',$v);
                if($return = $this->wechat->uploadImg($up)){
                    $this->weixin_pic[$k] = $return['url'];
                }else{
                    $this->weixin_pic[$k] = '';
                    continue;
                }
            }
        }else{
            $this->pic_arr[] = $img;
            $up['media'] = '@'.getcwd().str_replace(config('domain.pic'),'',$img);
            $return = $this->wechat->uploadImg($up);
            $this->weixin_pic[] = $return['url'];
        }

        $data = array_combine($this->pic_arr, $this->weixin_pic);
        return $data;
    }

    /**
     * @param $img
     * @return mixed
     * 上传图文缩略图到微信
     */
    public function uploadThumb($img){
        $up['media'] = '@'.getcwd().str_replace(config('domain.pic'),'',$img);
        $type        = 'thumb';
        $data = $this->wechat->uploadMedia($up,$type);
        if($data){
            return $data['thumb_media_id'];
        }else{
            return $this->wechat->errMsg;
        }
    }
    public function uploadArticles($data){
        if($return = $this->wechat->uploadArticles($data)){
            $return['errcode'] = 0;
            return $return;
        }else{
            $return['errcode'] = $this->wechat->errCode;
            $return['errmsg']  = $this->wechat->errMsg;
            return $return;
        }
    }
    /**
     * @param $data
     * @return array
     * 群发消息给用户
     */
    public function sendMsg($data){
        if($return = $this->wechat->sendGroupMassMessage($data)){
            $return['errcode'] = 0;
            return $return;
        }else{
            return ['errmsg'=>$this->wechat->errMsg,'errcode'=>$this->wechat->errCode];
        }
    }

    /**
     * @param $data
     * @return array
     * 创建微信菜单
     */
    public function createMenu($data){
        if($this->wechat->deleteMenu()){//删除菜单
            if(!$this->wechat->createMenu($data)){
                return ['errmsg'=>$this->wechat->errMsg,'errcode'=>$this->wechat->errCode];
            }else{
                return ['errcode'=>0];
            }
        }else{
            return ['errmsg'=>$this->wechat->errMsg,'errcode'=>$this->wechat->errCode];
        }
    }
    public function getAuthToken(){
        if($return = $this->wechat->getOauthAccessToken()){
            return $return;
        }else{
            return false;
        }
    }

    /**
     * @param $data
     * @return bool
     * 发送预览消息
     */
    public function previewMassMessage($data){
        if($return = $this->wechat->previewMassMessage($data)){
            return $return;
        }else{
            return false;
        }
    }
    /**
     * @param $url
     * @return mixed
     * 获取权授链接
     */
    public function getAuthUrl($url,$state='',$scope='snsapi_base'){
        return $this->wechat->getOauthRedirect($url,$state,$scope);
    }
}